package com.sun.login.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.sun.lib.callback.LoginCallBack;
import com.sun.lib.constant.ConstantArouter;
import com.sun.lib.util.EventBusUtil;
import com.sun.lib.util.IntentUtil;
import com.sun.login.LoginManager;
import com.sun.login.bean.LoginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/*
 * Created by Administrator on 2019/7/30.
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public class LoginBackFragment extends Fragment {
    private LoginCallBack callBack;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        EventBusUtil.register(this);
    }

    public void login(LoginCallBack callBack) {
        this.callBack = callBack;
        if (LoginManager.getInstance().isLogin()) {
            callBack.onSuccess();
            return;
        }

        IntentUtil.startActivity(getActivity(), ConstantArouter.PATH_LOGIN_ACTIVITY);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLoginEvent(LoginEvent event) {
        if (!isDetached() && callBack != null)
            if (event.isLoginStatus())
                callBack.onSuccess();
    }

    @Override
    public void onDestroy() {
        EventBusUtil.unregister(this);
        super.onDestroy();
    }
}
